//
//  BEComparableExtension.swift
//  BasicExtentions
//
//  Created by JY_NEW on 2021/11/4.
//

import Foundation
import UIKit

// MARK: Comparable
extension Comparable {
    ///若当前值在两个值中间则取当前值，若超过最大值则取最大值，小于最小值则取最小值
    public func fit(between v1: Self, _ v2: Self) -> Self {
        let min = min(v1, v2)
        let max = max(v1, v2)
        if self < min {
            return min
        }else if self > max {
            return max
        }
        return self
    }
    ///判断当前值是否在两个值中间，等于也算
    public func isBetween(_ v1: Self, _ v2: Self) -> Bool {
        let min = min(v1, v2)
        let max = max(v1, v2)
        return self >= min && self <= max
    }
    ///判断当前值是否在两个值中间，等于不算
    public func isInside(_ v1: Self, _ v2: Self) -> Bool {
        let min = min(v1, v2)
        let max = max(v1, v2)
        return self > min && self < max
    }
}

// MARK: NSRange
extension NSRange {
    /// 偏移位置
    public mutating func offset(_ value: Int) {
        self.location += value
    }
    /// 偏移位置
    public func byOffset(_ value: Int) -> NSRange {
        return NSMakeRange(self.location + value, self.length)
    }
}

// MARK: Bool
extension Bool {
    public mutating func reverse() {
        self = !self
    }
    public func byReverse() -> Self {
        return !self
    }
}

// MARK: NSNumber

/// 可转化为NSNumber
public protocol BENumberConvertible {
    var asNumber: NSNumber { get }
}
extension CChar: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension UInt8: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension UInt16: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension UInt64: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Float: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Int32: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Double: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Bool: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Int16: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension UInt32: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Int: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension UInt: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Int64: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension Decimal: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension CGFloat: BENumberConvertible {
    public var asNumber: NSNumber {
        return self as NSNumber
    }
}
extension BENumberConvertible {
    ///千分符表示法
    public func thousandsDescription(_ decimalLength:Int = 0) -> String {
        let formatter = NumberFormatter()
        let format = NSMutableString(string:"###,##0")
        if (decimalLength > 0) {
            format.append(".")
            for _ in 0...decimalLength - 1 {
                format.append("0")
            }
        }
        formatter.positiveFormat = format as String?
        return formatter.string(from: self.asNumber) ?? (decimalLength > 0 ? "0" + format.substring(from: 7) : "0")
    }
    ///格式化字符串
    public func format(_ format:String) -> String {
        let formatter = NumberFormatter()
        formatter.positiveFormat = format
        return formatter.string(from: self.asNumber) ?? self.asNumber.description
    }
}

// MARK: 弧度、角度

extension BERadiansComputable {
    ///自身为弧度，转为角度
    public static func radians(for degrees: Self) -> Self {
        return degrees * Self.pi / 180
    }
    ///自身角度，转为弧度
    public static func degrees(for radians: Self) -> Self  {
        return radians * 180 / Self.pi
    }
    ///自身角度，转为弧度
    public var asRadians: Self {
        return Self.radians(for: self)
    }
    ///自身为弧度，转为角度
    public var asDegrees: Self {
        return Self.degrees(for: self)
    }
}

// MARK: CGFloat、Decimal

extension CGFloat {
    ///单个像素值
    public static var pixel: CGFloat {
        return pixels(1)
    }
    ///计算像素值
    public static func pixels(_ p:CGFloat) -> CGFloat{
        return p / UIScreen.main.scale
    }
}
extension Decimal {
    /// 四舍五入
    public func round(_ scale:Int, mode:NSDecimalNumber.RoundingMode) -> Decimal {
        var result = Decimal()
        var source = self
        NSDecimalRound(&result, &source, scale, mode)
        return result
    }
}

// MARK: CGSize、CGPint、CGRect、UIEdgeInsets

extension CGSize {
    public init(_ widthAndHeight: CGFloat) {
        self.init(width: widthAndHeight, height: widthAndHeight)
    }
    public init(_ widthAndHeight: Int) {
        self.init(width: widthAndHeight, height: widthAndHeight)
    }
    public init(_ widthAndHeight: Double) {
        self.init(width: widthAndHeight, height: widthAndHeight)
    }
    public init(_ width: CGFloat, _ height: CGFloat) {
        self.init(width: width, height: height)
    }
    public init(_ width: Int, _ height: Int) {
        self.init(width: width, height: height)
    }
    public init(_ width: Double, _ height: Double) {
        self.init(width: width, height: height)
    }
    ///返回高宽中比较大的值
    public var max: CGFloat {
        return Swift.max(self.width, self.height)
    }
    ///返回高宽中比较小的值
    public var min: CGFloat {
        return Swift.min(self.width, self.height)
    }
    /// 是否竖向
    public var isPortrait: Bool {
        return self.width <= self.height
    }
    /// 是否横向
    public var isLandscape: Bool {
        return self.width > self.height
    }
    /// 转为CGPoint
    public func asPoint() -> CGPoint {
        return .init(x: self.width, y: self.height)
    }
    /// 转为CGRect
    public func asRect(_ origin: CGPoint) -> CGRect {
        return .init(origin: origin, size: self)
    }
    /// 转为CGRect
    public func asRect(x: CGFloat = 0, y: CGFloat = 0) -> CGRect {
        return .init(origin: .init(x: x, y: y), size: self)
    }
}
extension CGPoint {
    public init(_ xy: CGFloat) {
        self.init(x: xy, y: xy)
    }
    public init(_ xy: Int) {
        self.init(x: xy, y: xy)
    }
    public init(_ xy: Double) {
        self.init(x: xy, y: xy)
    }
    public init(_ x: CGFloat, _ y: CGFloat) {
        self.init(x: x, y: y)
    }
    public init(_ x: Int, _ y: Int) {
        self.init(x: x, y: y)
    }
    public init(_ x: Double, _ y: Double) {
        self.init(x: x, y: y)
    }
    public init(x: CGFloat) {
        self.init(x: x, y: 0)
    }
    public init(x: Int) {
        self.init(x: x, y: 0)
    }
    public init(x: Double) {
        self.init(x: x, y: 0)
    }
    public init(y: CGFloat) {
        self.init(x: 0, y: y)
    }
    public init(y: Int) {
        self.init(x: 0, y: y)
    }
    public init(y: Double) {
        self.init(x: 0, y: y)
    }
    ///返回xy中比较大的值
    public var max: CGFloat {
        return Swift.max(self.x, self.y)
    }
    ///返回xy中比较小的值
    public var min: CGFloat {
        return Swift.min(self.x, self.y)
    }
    /// 判断是否在指定的方框里面，贴边不算
    public func isInside(_ rect: CGRect) -> Bool {
        return self.x > rect.left && self.x < rect.right && self.y > rect.top && self.y < rect.bottom
    }
    /// 转为CGSize
    public func asSize() -> CGSize {
        return .init(width: self.x, height: self.y)
    }
}
extension CGRect {
    public init(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) {
        self.init(x: x, y: y, width: width, height: height)
    }
    public init(_ width: CGFloat,_ height: CGFloat) {
        self.init(x: 0, y: 0, width: width, height: height)
    }
    public init(_ size: CGSize) {
        self.init(origin: .zero, size: size)
    }
    ///上坐标，等于y
    public var top: CGFloat {
        return self.origin.y
    }
    ///左坐标，等于x
    public var left: CGFloat {
        return self.origin.x
    }
    ///下坐标，等于y + height
    public var bottom: CGFloat {
        return self.origin.y + self.size.height
    }
    ///右坐标，等于x + width
    public var right: CGFloat {
        return self.origin.x + self.size.width
    }
    /// 是否竖向
    public var isPortrait: Bool {
        return self.size.isPortrait
    }
    /// 是否横向
    public var isLandscape: Bool {
        return self.size.isLandscape
    }
    /// 根据锚点(实际数值(起点为0))扩张，默认为中间往四边扩张
    public mutating func expand(size: CGSize, anchor: CGPoint? = nil) {
        let offsetX = size.width - self.size.width
        let offsetY = size.height - self.size.height
        let x: CGFloat
        let y: CGFloat
        if let anchor = anchor {
            x = anchor.x / self.size.width
            y = anchor.y / self.size.height
        }else{
            x = 0.5
            y = 0.5
        }
        self.origin.x -= offsetX * x
        self.origin.y -= offsetY * y
        self.size = size
    }
    /// 根据锚点(相对数值0 - 1)按指定倍数扩张，默认为中间往四边扩张
    public mutating func expand(scale: CGSize, anchor: CGPoint? = nil) {
        let size = self.size * scale
        let point: CGPoint
        if let anchor = anchor {
            point = self.size.asPoint() * anchor
        }else {
            point = self.size.asPoint() * 0.5
        }
        self.expand(size: size, anchor: point)
    }
    /// 根据锚点(实际数值(起点为0))扩张，默认为中间往四边扩张
    public func asExpanded(size: CGSize, anchor: CGPoint?) -> CGRect {
        var rect = self
        rect.expand(size: size, anchor: anchor)
        return rect
    }
    /// 根据锚点(相对数值0 - 1)按指定倍数扩张，默认为中间往四边扩张
    public func asExpanded(scale: CGSize, anchor: CGPoint? = nil) -> CGRect {
        var rect = self
        rect.expand(scale: size, anchor: anchor)
        return rect
    }
    public func fit(size: CGSize, contentMode: UIView.ContentMode) -> CGRect {
        var rect = self.standardized
        var size = CGSize(abs(size.width), abs(size.height))
        let center = CGPoint(rect.midX, rect.midY)
        switch contentMode {
        case .scaleAspectFit, .scaleAspectFill:
            if (rect.size.width < 0.01 || rect.size.height < 0.01 ||
                size.width < 0.01 || size.height < 0.01) {
                rect.origin = center
                rect.size = .zero
            } else {
                let scale: CGFloat
                if (contentMode == .scaleAspectFit) {
                    if (size.width / size.height < rect.size.width / rect.size.height) {
                        scale = rect.size.height / size.height
                    } else {
                        scale = rect.size.width / size.width
                    }
                } else {
                    if (size.width / size.height < rect.size.width / rect.size.height) {
                        scale = rect.size.width / size.width
                    } else {
                        scale = rect.size.height / size.height
                    }
                }
                size.width *= scale
                size.height *= scale
                rect.size = size
                rect.origin = .init(center.x - size.width * 0.5, center.y - size.height * 0.5)
            }
        case .center:
            rect.size = size
            rect.origin = .init(center.x - size.width * 0.5, center.y - size.height * 0.5)
        case .top:
            rect.origin.x = center.x - size.width * 0.5
            rect.size = size
        case .bottom:
            rect.origin.x = center.x - size.width * 0.5
            rect.origin.y += rect.size.height - size.height
            rect.size = size
        case .left:
            rect.origin.y = center.y - size.height * 0.5
            rect.size = size
        case .right:
            rect.origin.y = center.y - size.height * 0.5
            rect.origin.x += rect.size.width - size.width
            rect.size = size
        case .topLeft:
            rect.size = size
        case .topRight:
            rect.origin.x += rect.size.width - size.width
            rect.size = size
        case .bottomLeft:
            rect.origin.y += rect.size.height - size.height
            rect.size = size
        case .bottomRight:
            rect.origin.x += rect.size.width - size.width
            rect.origin.y += rect.size.height - size.height
            rect.size = size
        case .scaleToFill, .redraw:
            break
        @unknown default:
            break
        }
        return rect
    }
}
extension UIEdgeInsets {
    public init(x: CGFloat = 0, y: CGFloat = 0) {
        self.init(top: y, left: x, bottom: y, right: x)
    }
    public init(_ all: CGFloat) {
        self.init(top: all, left: all, bottom: all, right: all)
    }
    public static func top(_ top: CGFloat) -> Self {
        self.init(top: top, left: 0, bottom: 0, right: 0)
    }
    public static func left(_ left: CGFloat) -> Self {
        self.init(top: 0, left: left, bottom: 0, right: 0)
    }
    public static func bottom(_ bottom: CGFloat) -> Self {
        self.init(top: 0, left: 0, bottom: bottom, right: 0)
    }
    public static func right(_ right: CGFloat) -> Self {
        self.init(top: 0, left: 0, bottom: 0, right: right)
    }
    public func top(_ top: CGFloat) -> Self {
        Self.init(top: top, left: self.left, bottom: self.bottom, right: self.right)
    }
    public func left(_ left: CGFloat) -> Self {
        Self.init(top: self.top, left: left, bottom: self.bottom, right: self.right)
    }
    public func bottom(_ bottom: CGFloat) -> Self {
        Self.init(top: self.top, left: self.left, bottom: bottom, right: self.right)
    }
    public func right(_ right: CGFloat) -> Self {
        Self.init(top: self.top, left: self.left, bottom: self.bottom, right: right)
    }
    public var leftRight: CGFloat {
        return self.left + self.right
    }
    public var topBottom: CGFloat {
        return self.top + self.bottom
    }
}

//// MARK: CGSize、CGPint、CGRect、UIEdgeInsets 赋值与计算

extension CGSize {
    public static func + (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.width + rhs, lhs.height + rhs)
    }
    public static func - (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.width - rhs, lhs.height - rhs)
    }
    public static func * (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.width * rhs, lhs.height * rhs)
    }
    public static func / (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.width / rhs, lhs.height / rhs)
    }
    public static func + (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.width + rhs.width, lhs.height + rhs.height)
    }
    public static func - (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.width - rhs.width, lhs.height - rhs.height)
    }
    public static func * (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.width * rhs.width, lhs.height * rhs.height)
    }
    public static func / (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.width / rhs.width, lhs.height / rhs.height)
    }
}
extension CGPoint {
    public static func + (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.x + rhs, lhs.y + rhs)
    }
    public static func - (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.x - rhs, lhs.y - rhs)
    }
    public static func * (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.x * rhs, lhs.y * rhs)
    }
    public static func / (lhs: Self, rhs: CGFloat) -> Self {
        return .init(lhs.x / rhs, lhs.y / rhs)
    }
    public static func + (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.x + rhs.x, lhs.y + rhs.y)
    }
    public static func - (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.x - rhs.x, lhs.y - rhs.y)
    }
    public static func * (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.x * rhs.x, lhs.y * rhs.y)
    }
    public static func / (lhs: Self, rhs: Self) -> Self {
        return .init(lhs.x / rhs.x, lhs.y / rhs.y)
    }
    public static func + (lhs: Self, rhs: CGSize) -> CGRect {
        return .init(origin: lhs, size: rhs)
    }
    public static func + (lhs: CGSize, rhs: Self) -> CGRect {
        return .init(origin: rhs, size: lhs)
    }
    public static prefix func -(point: CGPoint) -> CGPoint {
        return .init(-point.x, -point.y)
    }
}

extension CGRect {
    public static func + (lhs: Self, rhs: CGPoint) -> Self {
        return .init(origin: lhs.origin + rhs, size: lhs.size)
    }
    public static func - (lhs: Self, rhs: CGPoint) -> Self {
        return .init(origin: lhs.origin - rhs, size: lhs.size)
    }
    public static func * (lhs: Self, rhs: CGPoint) -> Self {
        return .init(origin: lhs.origin * rhs, size: lhs.size)
    }
    public static func / (lhs: Self, rhs: CGPoint) -> Self {
        return .init(origin: lhs.origin / rhs, size: lhs.size)
    }
    public static func + (lhs: Self, rhs: CGSize) -> Self {
        return .init(origin: lhs.origin, size: lhs.size + rhs)
    }
    public static func - (lhs: Self, rhs: CGSize) -> Self {
        return .init(origin: lhs.origin, size: lhs.size - rhs)
    }
    public static func * (lhs: Self, rhs: CGSize) -> Self {
        return .init(origin: lhs.origin, size: lhs.size * rhs)
    }
    public static func / (lhs: Self, rhs: CGSize) -> Self {
        return .init(origin: lhs.origin, size: lhs.size / rhs)
    }
    public static func + (lhs: Self, rhs: Self) -> Self {
        return .init(origin: lhs.origin + rhs.origin, size: lhs.size + rhs.size)
    }
    public static func - (lhs: Self, rhs: Self) -> Self {
        return .init(origin: lhs.origin - rhs.origin, size: lhs.size - rhs.size)
    }
    public static func * (lhs: Self, rhs: Self) -> Self {
        return .init(origin: lhs.origin * rhs.origin, size: lhs.size * rhs.size)
    }
    public static func / (lhs: Self, rhs: Self) -> Self {
        return .init(origin: lhs.origin / rhs.origin, size: lhs.size / rhs.size)
    }
}

extension UIEdgeInsets {
    public static func + (lhs: Self, rhs: CGFloat) -> Self {
        return .init(top: lhs.top + rhs, left: lhs.left + rhs, bottom: lhs.bottom + rhs, right: lhs.right + rhs)
    }
    public static func - (lhs: Self, rhs: CGFloat) -> Self {
        return .init(top: lhs.top - rhs, left: lhs.left - rhs, bottom: lhs.bottom - rhs, right: lhs.right - rhs)
    }
    public static func * (lhs: Self, rhs: CGFloat) -> Self {
        return .init(top: lhs.top * rhs, left: lhs.left * rhs, bottom: lhs.bottom * rhs, right: lhs.right * rhs)
    }
    public static func / (lhs: Self, rhs: CGFloat) -> Self {
        return .init(top: lhs.top / rhs, left: lhs.left / rhs, bottom: lhs.bottom / rhs, right: lhs.right / rhs)
    }
    public static func + (lhs: Self, rhs: Self) -> Self {
        return .init(top: lhs.top + rhs.top, left: lhs.left + rhs.left, bottom: lhs.bottom + rhs.bottom, right: lhs.right + rhs.right)
    }
    public static func - (lhs: Self, rhs: Self) -> Self {
        return .init(top: lhs.top - rhs.top, left: lhs.left - rhs.left, bottom: lhs.bottom - rhs.bottom, right: lhs.right - rhs.right)
    }
    public static func * (lhs: Self, rhs: Self) -> Self {
        return .init(top: lhs.top * rhs.top, left: lhs.left * rhs.left, bottom: lhs.bottom * rhs.bottom, right: lhs.right * rhs.right)
    }
    public static func / (lhs: Self, rhs: Self) -> Self {
        return .init(top: lhs.top / rhs.top, left: lhs.left / rhs.left, bottom: lhs.bottom / rhs.bottom, right: lhs.right / rhs.right)
    }
}

extension CGSize {
    public static func += (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs / rhs
    }
    public static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: Self) {
        lhs = lhs / rhs
    }
}

extension CGPoint {
    public static func += (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs / rhs
    }
    public static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: Self) {
        lhs = lhs / rhs
    }
}

extension CGRect {
    public static func += (lhs: inout Self, rhs: CGPoint) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: CGPoint) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: CGPoint) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: CGPoint) {
        lhs = lhs / rhs
    }
    public static func += (lhs: inout Self, rhs: CGSize) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: CGSize) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: CGSize) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: CGSize) {
        lhs = lhs / rhs
    }
    public static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: Self) {
        lhs = lhs / rhs
    }
}

extension UIEdgeInsets {
    public static func += (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: CGFloat) {
        lhs = lhs / rhs
    }
    public static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }
    public static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }
    public static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }
    public static func /= (lhs: inout Self, rhs: Self) {
        lhs = lhs / rhs
    }
}

extension CGAffineTransform {
    public enum Axis {
        /// 横向
        case horizontal
        /// 竖向
        case vertical
    }
    /// 翻转
    public init(flip axis: Axis) {
        switch axis {
        case .horizontal:
            self.init(scaleX: -1, y: 1)
        case .vertical:
            self.init(scaleX: 1, y: -1)
        }
    }
    /// 翻转
    public func flippedBy(_ axis: Axis) -> Self {
        switch axis {
        case .horizontal:
            return self.scaledBy(x: -1, y: 1)
        case .vertical:
            return self.scaledBy(x: 1, y: -1)
        }
    }
}
